import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/home/news_comment_entity.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/view/expert/items/expert_logo.dart';
import 'package:sports_new/view/home/comments/comment_sheet.dart';
import 'package:sports_new/widgets/asset_item.dart';
import 'package:sports_new/widgets/base/qkd_text.dart';
import 'package:sports_new/widgets/card.dart';
import 'package:sports_new/widgets/circular.dart';
import '../../../resources/colours.dart';
import '../../../utils/utils.dart';
import '../../../widgets/base/qkd_dialog.dart';
import '../../../widgets/select_bottomsheet.dart';

class CommentItem extends StatelessWidget {
  const CommentItem({super.key,
    this.comment,
    this.onLike,
    this.onReply,
    this.onDelete,
    this.onReport,
    required this.newsId,
    this.isSecondary = false,
    this.isOrigin = false
  });

  final NewsCommentEntity? comment;
  final int newsId;
  final bool isSecondary;
  final bool isOrigin;
  final ValueChanged<int>? onLike;
  final Function()? onReply;
  final Function()? onDelete;
  final ValueChanged<String>? onReport;

  @override
  Widget build(BuildContext context) {
    return Block(
      onTap: () {
        if(isSecondary) {
          onReply?.call();
        }else{
          CommentSheet.show(comment, newsId,false);
        }
      },
      cardStyle: const CardStyle(
        padding: EdgeInsets.symmetric(vertical: 10),
        color: Colours.white,
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          GestureDetector(
            onTap: () => Get.toNamed(
              Routes.userPage,
              arguments: comment?.userId,
              parameters: {"type":"1"}
            ),
            child: ExpertLogo(
              logo: comment?.userLogo,
              size: 32,
            ),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 32,
                  child: Row(
                    children: [
                      GestureDetector(
                        onTap: () => Get.toNamed(
                            Routes.userPage,
                            arguments: comment?.userId,
                            parameters: {"type":"1"}
                        ),
                        child: QkdText(
                          comment?.userName??"",
                          fontSize: 16, fontWeight: FontWeight.w500
                        ),
                      ),
                      const Spacer(),
                      AssetItem(
                        padding: const EdgeInsets.only(left: 5),
                        direction: AssetDirection.ltr,
                        asset: comment?.isLike == 1?'zan_red.png':'zan.png',
                        name: (comment?.likeNum.hasValue??false)?"${comment?.likeNum??0}":null,
                        assetSize: 14,
                        space: 4,
                        textStyle: TextStyle(
                          fontSize: 13,
                          color: comment?.isLike == 1 ? Colours.main : Colours.grey99
                        ),
                        onTap: () {
                          onLike?.call(comment?.isLike == 1?0:1);
                        },
                      )
                    ],
                  ).paddingOnly(left: 8),
                ),
                if(comment?.fromComment.hasValue??false)
                  Circular(
                    width: Get.width,
                    color: Colours.greyF5F7FA,
                    radius: 4,
                    margin: const EdgeInsets.only(left: 4,top: 8),
                    padding: const EdgeInsets.all(8),
                    child: QkdText(
                      comment?.fromComment??"",
                      fontSize: 16,
                      color: Colours.grey66
                    ),
                  ),
                if(comment?.special.hasValue??false)
                  QkdText(
                    comment?.special??"",
                    fontSize: 16,
                    color: Colours.grey99
                  )
                else
                  QkdText(
                    comment?.comment??"",
                    fontSize: 16,
                    maxLines: 99,
                  ).paddingOnly(
                    left: 8,top: 4,bottom: 10
                  ),
                Row(
                  children: [
                    GestureDetector(
                      onTap: () {
                        if(isSecondary){
                          onReply?.call();
                        }else {
                          CommentSheet.show(comment, newsId, true);
                        }
                      },
                      child: Circular(
                        color: Colours.greyF2,
                        radius: 12,
                        padding: const EdgeInsets.symmetric(horizontal: 8,vertical: 4),
                        child: AssetItem(
                          direction: AssetDirection.rtl,
                          asset: "arrow_right.png",
                          name: "${comment?.commentNum.hasValue??false?comment?.commentNum:""}回复",
                          assetSize: 10,
                          textStyle: const TextStyle(
                            fontSize: 12,color: Colours.text
                          ),
                        ),
                      ),
                    ),
                    Container(width: 10),
                    QkdText(
                      "${comment?.createTime??""} · 来自${comment?.addr??""}",
                      fontSize: 12,color: Colours.grey99,
                    ),
                    const Spacer(),
                    if(!isOrigin)
                    if(comment?.isUser == 1)
                      GestureDetector(
                        onTap: () async{
                          final r = await Get.dialog(
                            const QkdDialog.alert("确认删除评论吗?")
                          ) ?? false;
                          if(r == true) onDelete?.call();
                        },
                        child: Container(
                          alignment: Alignment.centerRight,
                          width: 50,height: 20,
                          child: const QkdText(
                            "删除",
                            fontSize: 12,color: Colours.grey99
                          )
                        ),
                      )
                    else
                      GestureDetector(
                        onTap: () async{
                          final opts = ["淫秽色情", "营销广告", "网络暴力", "违法信息", "虚假谣言"];
                          int? i = await Get.bottomSheet(SelectBottomSheet(opts));
                          if(i != null) onReport?.call(opts[i]);
                        },
                        child: Container(
                          alignment: Alignment.centerRight,
                          width: 50,
                          height: 20,
                          child: Image.asset(
                            Utils.getImgPath("post_close.png"),
                            width: 10, height: 10
                          ),
                        ),
                      )
                  ],
                ).paddingOnly(left: 8)
              ],
            ),
          )
        ],
      ),
    );
  }
}
